package com.itboye.textileexpres.top;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.textileexpes.presenter.GoodsPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.alipaytest.ActivityPaymentMethod;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.main.MainActivity;
import com.itboye.textileexpres.myset.ActivityCustomerDetail;
import com.itboye.textileexpres.util.BaseDialogFragment;
import com.itboye.textileexpres.util.MyToast;
import com.itboye.textileexpres.widget.CarouselView;
import com.itboye.textileexpres.widget.McoyProductContentPage;
import com.itboye.textileexpres.widget.McoyProductDetailInfoPage;
import com.itboye.textileexpres.widget.McoySnapPageLayout;
import com.itboye.textileexpres.widget.PagerSlidingTabStrip;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.Animator.AnimatorListener;
import com.nineoldandroids.animation.ObjectAnimator;

public class GoodsDetailsActivity extends BaseActivity implements
		OnClickListener, Observer {
	private McoySnapPageLayout flipLayout;
	ViewPager pager;
	PagerSlidingTabStrip tabs;
	McoyProductContentPage twoPage;
	McoyProductDetailInfoPage firstpage;
	ImageView back;
	TextView title;
	ImageView img_other;
	ImageView img_search;
	PopupWindow popupWindow;
	LinearLayout share_tv, new_tv, main_tv;
	LinearLayout shopcart_lin, buy_lin;
	CarouselView topLunbo;
	private List<ImageView> bmps;
	String pid;
	RelativeLayout top_rel;
	GoodsPresenter presenter;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_goods_detail;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		title.setText(R.string.product);
		pid = getIntent().getStringExtra("pid");
		presenter = new GoodsPresenter(this);
		presenter.DetailQuery("22");
		Log.e("hu.detaile", pid + "");
		img_search.setImageResource(R.drawable.goods_shopcart);
		img_other.setImageResource(R.drawable.goods_more);
		firstpage = new McoyProductDetailInfoPage(GoodsDetailsActivity.this,
				getLayoutInflater().inflate(R.layout.mcoy_produt_detail_layout,
						null));
		twoPage = new McoyProductContentPage(GoodsDetailsActivity.this,
				getLayoutInflater().inflate(R.layout.mcoy_product_content_page,
						null));
		flipLayout.setSnapPages(firstpage, twoPage);
		pager = (ViewPager) flipLayout.findViewById(R.id.pager);
		img_other.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				View popView = LayoutInflater.from(GoodsDetailsActivity.this)
						.inflate(R.layout.pop_goods_detail, null);
				share_tv = (LinearLayout) popView.findViewById(R.id.share_lin);
				new_tv = (LinearLayout) popView.findViewById(R.id.new_lin);
				main_tv = (LinearLayout) popView.findViewById(R.id.main_lin);
				share_tv.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub

						// Toast(GoodsDetailsActivity.this, "share");
						popupWindow.dismiss();
					}
				});
				new_tv.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						toast("new");
						popupWindow.dismiss();
					}
				});
				main_tv.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(GoodsDetailsActivity.this,
								MainActivity.class);
						startActivity(intent);
						popupWindow.dismiss();
					}
				});
				popupWindow = new PopupWindow(popView, 180,
						LayoutParams.WRAP_CONTENT);
				popupWindow.setOutsideTouchable(true);
				popupWindow.setBackgroundDrawable(new BitmapDrawable());
				popupWindow.showAsDropDown(v);

			}
		});
		shopcart_lin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new JiaRuGouWuCheDialog().show(getSupportFragmentManager(), "");
			}
		});
		onBaner();
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == GoodsPresenter.Detail_success) {
				MyToast.toast("" + entity.getData());

			} else if (entity.getEventType() == GoodsPresenter.Detail_fail) {
				MyToast.toast("" + entity.getData());
			}
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent = null;
		switch (v.getId()) {
		case R.id.back:

			finish();
			break;
		case R.id.buy_lin:
			intent = new Intent(GoodsDetailsActivity.this,
					ActivityPaymentMethod.class);
			startActivity(intent);
			break;
		case R.id.img_search:
			intent = new Intent(GoodsDetailsActivity.this,
					ActivityCustomerDetail.class);
			startActivity(intent);
			break;
		default:
			break;
		}
	}

	/****
	 * @author �ֲ�
	 */
	public void onBaner() {
		// DisplayMetrics dm = new DisplayMetrics();
		// getWindowManager().getDefaultDisplay().getMetrics(dm);
		// // 窗口的宽度
		// Window window = getWindow();
		// LayoutParams params = window.getAttributes();
		// // LayoutParams vp = (LayoutParams) top_rel.getLayoutParams();
		// // int screenWidth = dm.widthPixels;
		// params.width = dm.widthPixels;
		// params.height = params.width * 3 / 4;
		// top_rel.setLayoutParams(params);
		// // android.widget.RelativeLayout.LayoutParams lP = new
		// // android.widget.RelativeLayout.LayoutParams(
		// // ChangeToDP(screenWidth), ChangeToDP(screenWidth * 3 / 4));
		topLunbo = (CarouselView) findViewById(R.id.topLunbo);
		bmps = new ArrayList<ImageView>();
		for (int i = 0; i < 5; i++) {
			ImageView imageView = new ImageView(this);
			imageView.setScaleType(ScaleType.FIT_XY);

			com.nostra13.universalimageloader.core.ImageLoader
					.getInstance()
					.displayImage(
							"http://cdn.duitang.com/uploads/blog/201402/05/20140205123459_uaCui.thumb.600_0.jpeg",
							imageView);
			bmps.add(imageView);

		}
		topLunbo.setImageBitmaps(bmps);
	}

	class JiaRuGouWuCheDialog extends BaseDialogFragment {
		View close;
		ImageView pic;
		TextView jiege;
		TextView kucun;
		ViewGroup guigeContainer;
		View ok;
		View jian;
		View jia;
		EditText input;
		boolean isLiJIGouMai;
		ViewGroup container;
		String imgUrl;
		protected String price;
		protected String skuId;

		public JiaRuGouWuCheDialog() {

		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {

			return LayoutInflater.from(GoodsDetailsActivity.this).inflate(
					R.layout.dialog_add_shopcart, null);
		}

		private TextView createTitle(String s) {
			TextView textView = new TextView(GoodsDetailsActivity.this);
			textView.setText(s);
			textView.setTextColor(Color.BLACK);
			return textView;
		}

		public void close() {
			View v = getView().findViewById(R.id.container);
			ObjectAnimator animator = ObjectAnimator.ofFloat(v, "translationY",
					0, v.getHeight()).setDuration(300);
			animator.addListener(new AnimatorListener() {
				@Override
				public void onAnimationStart(Animator arg0) {
				}

				@Override
				public void onAnimationRepeat(Animator arg0) {
				}

				@Override
				public void onAnimationEnd(Animator arg0) {
					dismiss();
				}

				@Override
				public void onAnimationCancel(Animator arg0) {
					dismiss();
				}
			});
			animator.start();
		}

		@Override
		public void onActivityCreated(Bundle savedInstanceState) {

			super.onActivityCreated(savedInstanceState);

			if (savedInstanceState != null) {
				// goodsDetailBeans = (GoodsDetailsRootBean) savedInstanceState
				// .getSerializable("goodsDetailBeans");
				// isLiJIGouMai = savedInstanceState.getBoolean("isLiJIGouMai");
			}
			jia.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					String s = input.getText().toString();
					try {
						int c = Integer.parseInt(s);
						input.setText(c + 1 + "");
					} catch (NumberFormatException e) {
						input.setText("1");
					}
					input.setSelection(input.getText().length());
				}
			});
			jian.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					String s = input.getText().toString();
					try {
						int c = Integer.parseInt(s);
						if (c > 0) {
							c--;
						}
						input.setText(c + "");
					} catch (NumberFormatException e) {
						input.setText("0");
					}
					input.setSelection(input.getText().length());
				}
			});
			close.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					close();
				}
			});
			ok.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					dismiss();
				}
			});

		}

	}

}
